package com.dfd.store.web.servlet;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dfd.store.domain.Order;
import com.dfd.store.domain.PageModel;
import com.dfd.store.service.OrderService;
import com.dfd.store.service.ProductService;
import com.dfd.store.service.impl.OrderServiceImpl;
import com.dfd.store.service.impl.ProductServiceImpl;
import com.dfd.store.web.base.BaseServlet;

import net.sf.json.JSONArray;

public class AdminOrderServlet extends BaseServlet {
	public String findAll(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//接收参数
		String value = req.getParameter("state");
		// 调用业务逻辑层
		OrderService orderService = new OrderServiceImpl();
		//List<Order> orders = orderService.findAll();
		List<Order> orders = null;
		if(value == null){
			//查询所有
			orders = orderService.findAll();
		}else{
			int state = Integer.parseInt(value);
			orders =  orderService.findByState(state); 
		
		}
		
		// 将orders放入request中
		req.setAttribute("orders", orders);
		return "/admin/order/list.jsp";

	}
	public String findOrderByid(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//获取参数
		String id = req.getParameter("id");
		//查询
		OrderService orderService = new OrderServiceImpl();
		Order order = orderService.findOrderByOid(id);
		//将返回的集合转换为Json格式
		String jsonstr = JSONArray.fromObject(order.getList()).toString();
		System.out.println(JSONArray.fromObject(order.getList()).toString());
		//响应到客户端
		resp.setContentType("application/json;charset=utf-8");
		resp.getWriter().println(jsonstr);
		return null;
	}


}
